package com.ljc.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljc.common.ErrorCode;
import com.ljc.exception.BusinessException;
import com.ljc.mapper.InterfaceChargingMapper;
import com.ljc.model.dto.UnLockAvailablePiecesDTO;
import com.ljc.model.entity.InterfaceCharging;
import com.ljc.service.InterfaceChargingService;
import org.springframework.stereotype.Service;

/**
 * 针对表【interface_charging】的数据库操作Service实现
 */
@Service
public class InterfaceChargingServiceImpl extends ServiceImpl<InterfaceChargingMapper, InterfaceCharging> implements InterfaceChargingService {

    @Override
    public boolean checkInventory(Long id) {
        if (id == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        InterfaceCharging interfaceCharging = this.getById(id);
        return Integer.parseInt(interfaceCharging.getAvailablePieces()) >= 0;
    }

    @Override
    public boolean unLockAvailablePieces(UnLockAvailablePiecesDTO unLockAvailablePiecesDTO) {
        Long interfaceId = unLockAvailablePiecesDTO.getInterfaceId();
        Long count = unLockAvailablePiecesDTO.getCount();
        UpdateWrapper<InterfaceCharging> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("interfaceId", interfaceId).setSql("availablePieces = availablePieces + " + count);
        boolean update = this.update(updateWrapper);
        if (!update) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return update;
    }
}




